//
//  TFQskQuestions1to16.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/13.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TFQskQuestions1to16: UIView, UITableViewDelegate, UITableViewDataSource {

    

    @objc let textField = TFMsg()
    @objc let send = UIButton()
    @objc let tableView = UITableView()
    @objc let array = NSMutableArray()
    @objc let qskQuestionsLabel = UILabel()


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let string = NSMutableAttributedString.getAttributed(string: "请输入要提问的内容！", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 159/255.0, green: 163/255.0, blue: 168/255.0, alpha: 1))
        self.textField.attributedPlaceholder = string
        self.textField.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.textField.layer.cornerRadius = 16
        self.addSubview(self.textField)
        
        self.send.titleLabel!.font = UIFont.PingFang(size: 16)
        self.send.backgroundColor = UIColor.clear
        self.send.layer.cornerRadius = 16;
        self.addSubview(self.send)
       
        self.backgroundColor = UIColor(red: 21/255.0, green: 34/255.0, blue: 54/255.0, alpha: 1)
        self.tableView.backgroundColor = UIColor.clear
        //不要下划线
        self.tableView.separatorStyle = .none

        self.tableView.delegate   = self
        self.tableView.dataSource = self
        self.addSubview(self.tableView)
        

        self.addSubview(self.qskQuestionsLabel)
        self.qskQuestionsLabel.textAlignment = .center
        
        let qskQuestionsLabelString = NSMutableAttributedString.getAttributed(string: "无问答内容", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0, alpha: 1))
      
      self.qskQuestionsLabel.attributedText = qskQuestionsLabelString
  
        self.tableView.register(TFQskQuestions1to16Cell.self, forCellReuseIdentifier: "TFQskQuestions1to16Cell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.qskQuestionsLabel.frame = self.bounds
        self.textField.frame = CGRect(x: 15, y: self.frame.size.height - 15.5 - 32, width: self.frame.size.width - 30, height: 32)
        
        self.send.frame = self.textField.frame
        
        self.tableView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.textField.frame.origin.y-2)
      
    }
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.array.count>0) {
            
             self.qskQuestionsLabel.isHidden = true
        }else{
            self.qskQuestionsLabel.isHidden = false
          
        }
        
        return self.array.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "TFQskQuestions1to16Cell") as! TFQskQuestions1to16Cell

         let model =  self.array[indexPath.row] as! TFQuestionData
        
        cell.questionerAvatar.sd_setImage(with:URL.init(string: model.avatar))
       
         cell.viewWidth = self.frame.size.width
        
        
         cell.viewHeight = self.tableView(tableView, heightForRowAt: indexPath)
         cell.model = model
        cell.backgroundColor = UIColor.clear
        cell.selectionStyle = .none
         return cell
    }
    
    // 设置cell高度
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            
            let dict = self.array[indexPath.row] as! TFQuestionData
          
            
            let  size11 = TalkfunSharedTools.getSizeLabel(labelString: NSMutableAttributedString.init(string: dict.content), labelWidth: self.frame.size.width-55-15, labelfont: UIFont.PingFang(size: 14))
            
          
            //老师回复了
            
            if (dict.answer.count>0) {
                
                let answer = dict.answer as NSArray
                
                var contentHight:CGFloat = 0
                for i in 0..<answer.count {
                    let dict2 = answer[i] as! TFQuestionAnswer
               

                    let  size2 = TalkfunSharedTools.getSizeLabel(labelString: NSMutableAttributedString.init(string: dict2.content), labelWidth: self.frame.size.width-85-15, labelfont: UIFont.PingFang(size: 14))
                    
                    contentHight = contentHight+size2.height+20+10
                    
                    
                }
                
                let hight:CGFloat = contentHight
                
                let viewHeight:CGFloat = size11.height+36.5+20+9+hight+2

            //加上下间距
            return viewHeight
            }
            
            return size11.height+36.5+15+2
       
        }
    
    
    
    
    //回复删除
    
    @objc func replyTodelete(_ model:TFQuestionData)
    {

        
        let copyArray = NSMutableArray.init(array: self.array)
        for i in 0..<copyArray.count {
     
            let obj = copyArray[i] as! TFQuestionData
            
            if (obj.qid == model.qid) {
                
                
                //删除某个问题下的其中一个回复
                if (model.deleteQuestionID.count > 0) {
                    for i in 0..<obj.answer.count {
                       let answerModel = obj.answer[i] as! TFQuestionAnswer
                 
                        
                        
                        if (answerModel.qid == model.deleteQuestionID) {
                            obj.answer.remove(answerModel)
                          
                            break;
                            
                        }
                    }
                    
                }
                
                else{
                    //整个问题删除
                    self.array.remove(obj)
                    
                     break
                   
                }
                   
            }
           
            
        
        }
        self.tableView.reloadData()
      

    }
    //回复提问
    @objc func questionPeply(_ model:TFQuestionData)
    {
        let copyArray = NSMutableArray.init(array: self.array)
        var answer:Bool = false
        
        for i in 0..<copyArray.count {
      
            let obj = copyArray[i] as! TFQuestionData
            //本地自己的提问中是否查询到
             if ( obj.qid == model.qid) {
                 answer = true
                 self.array.replaceObject(at: i, with: model)
               
                
                break;
            }
            
        }
        
        //新增新的
        if (answer==false) {
            self.array.add(model)
        
            
        }
        self.tableView.reloadData()
        
     
    }
    //删除整个提问
    
    @objc func removeDict(_ model:TFQuestionData)

    {

        let copyArray = NSMutableArray.init(array: self.array)
        for i in 0..<copyArray.count {
            let obj = copyArray[i] as! TFQuestionData
            
            if (obj.qid == model.qid) {
                
                
                //删除某个问题下的其中一个回复
                if (model.deleteQuestionID.count > 0) {
                    for i in 0..<obj.answer.count {
                       let answerModel = obj.answer[i] as! TFQuestionAnswer
                    
                        if (answerModel.qid == model.deleteQuestionID) {
                            obj.answer.remove(answerModel)
                          
                            break;
                            
                        }
                    }
                    
                }
                
                else{
                    //整个问题删除
                    self.array.remove(obj)
                   
                     break
                   
                }
                   
            }
           
            
        
        }
        self.tableView.reloadData()
       
    }
    //提问通过
    @objc func addQskQuestionsDict(_ model:TFQuestionData)

    {
        for i in 0..<self.array.count {
          let  dict2 = self.array[i] as! TFQuestionData
 
            
          if (dict2.qid == model.qid) {
              return
          }
          
        }
        self.array.add(model)
        self.tableView.reloadData()
        
        let indexpath = NSIndexPath.init(row: self.array.count - 1, section: 0)
        
        self.tableView.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)

    }
    
    @objc func updatedQuestionListUI(_ array:NSArray)

    {
        self.array.removeAllObjects()
  
        
        self.array.addObjects(from: array as! [Any])
        self.tableView.reloadData()
        
         if (self.array.count>0) {
             
             let indexpath = NSIndexPath.init(row: self.array.count - 1, section: 0)
             
             self.tableView.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)
        }
         
       
    }

    //更新切换语言
   @objc func upDataLanguage()

    {
        
        let string = NSMutableAttributedString.getAttributed(string: "请输入要提问的内容！", font: UIFont.PingFang(size: 16), foregroundColor: UIColor(red: 159/255.0, green: 163/255.0, blue: 168/255.0, alpha: 1))
        self.textField.attributedPlaceholder = string
        
        let qskQuestionsLabelString = NSMutableAttributedString.getAttributed(string: "无问答内容", font: UIFont.PingFang(size: 18), foregroundColor: UIColor(red: 173/255.0, green: 204/255.0, blue: 222/255.0, alpha: 1))
        
        self.qskQuestionsLabel.attributedText = qskQuestionsLabelString
    }

}
